package com.slmxz.tms.common.util;

import cn.hutool.core.util.StrUtil;

/**
 * @Author：albert
 * @Date：2024/9/27 22:38
 */
public class StringExtendUtil {

    /**
     * @param para
     * @return
     */
    public static String humpToUnderline(String para) {

        para = lowerFirst(para);
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;

        for(int i = 0; i < para.length(); ++i) {
            if (Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                ++temp;
            }
        }

        return sb.toString().toLowerCase();
    }

    public static String lowerFirst(String str) {
        char firstChar = str.charAt(0);
        if (firstChar >= 'A' && firstChar <= 'Z') {
            char[] arr = str.toCharArray();
            arr[0] = (char)(arr[0] + 32);
            return new String(arr);
        } else {
            return str;
        }
    }

    public static String removeSuffix(CharSequence str, CharSequence suffix) {
        if (StrUtil.isNotBlank(str) && StrUtil.isNotBlank(suffix)) {
            String str2 = str.toString();
            return str2.endsWith(suffix.toString()) ? sub(str2, 0,str2.length() - suffix.length()) : str2;
        } else {
            return "";
        }
    }

    public static String sub(CharSequence str, int fromIndex, int toIndex) {
        if (StrUtil.isBlank(str)) {
            return "";
        } else {
            int len = str.length();
            if (fromIndex < 0) {
                fromIndex += len;
                if (fromIndex < 0) {
                    fromIndex = 0;
                }
            } else if (fromIndex > len) {
                fromIndex = len;
            }

            if (toIndex < 0) {
                toIndex += len;
                if (toIndex < 0) {
                    toIndex = len;
                }
            } else if (toIndex > len) {
                toIndex = len;
            }

            if (toIndex < fromIndex) {
                int tmp = fromIndex;
                fromIndex = toIndex;
                toIndex = tmp;
            }

            return fromIndex == toIndex ? "" : str.toString().substring(fromIndex, toIndex);
        }
    }
}
